package com.sky.controller.admin;

import com.sky.dto.*;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.OrdersService;
import com.sky.vo.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


@Slf4j
@RestController("adminOrderController")
@RequestMapping("/admin/order")
@Api(tags = "商家端订单接口相关")
public class OrdersController {


    @Autowired
    private OrdersService orderService;

    /**
     * 分页查询订单
     * @param pageQueryDTO
     * @return
     */
    @GetMapping("/conditionSearch")
    @ApiOperation("订单搜索")
    public Result<PageResult> conditionSearch(OrdersPageQueryDTO pageQueryDTO){
        log.info("查询订单：{}",pageQueryDTO);
        PageResult pageResult = orderService.adminPageOrders(pageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 各个状态的订单数量统计
     * @return
     */
    @GetMapping("/statistics")
    @ApiOperation("各个状态的订单数量统计")
    public Result<OrderStatisticsVO> statistics(){
        OrderStatisticsVO orderStatisticsVO=orderService.statistics();
        return Result.success(orderStatisticsVO);
    }

    /**
     * 查看订单详情
     * @param id
     * @return
     */
    @GetMapping("/details/{id}")
    @ApiOperation("查看订单详情")
    public Result<OrderVO> details(@PathVariable Long id){
        OrderVO order = orderService.getOrderById(id);
        return Result.success(order);
    }


    /**
     * 接收订单
     * @param id
     * @return
     */
    @PutMapping("/confirm")
    @ApiOperation("查看订单详情")
    public Result<String> confirm(@RequestBody OrdersRejectionDTO rejectionDTO){
        log.info("商家接单id:{},原因：{}",rejectionDTO.getId());
        orderService.updateConfirmById(rejectionDTO.getId());
        return Result.success("接单成功");
    }


    /**
     * 拒收订单
     * @param id
     * @return
     */
    @PutMapping("/rejection")
    @ApiOperation("拒收订单")
    public Result<String> rejection(@RequestBody OrdersRejectionDTO rejectionDTO){
        log.info("商家拒单id:{},原因：{}",rejectionDTO.getId(),rejectionDTO.getRejectionReason());
        orderService.updateRejectionById(rejectionDTO);
        return Result.success("拒收订单！");
    }



    /**
     * 取消订单
     * @param ordersCancelDTO
     * @return
     */
    @PutMapping ("/cancel")
    @ApiOperation("取消订单")
    public Result<String> cancel(@RequestBody OrdersCancelDTO ordersCancelDTO) throws Exception {
        orderService.cancel(ordersCancelDTO);
        return Result.success("取消订单成功！");
    }


    /**
     * 派送订单
     * @param id
     * @return
     */
    @PutMapping("/delivery/{id}")
    @ApiOperation("派送订单")
    public Result<String> delivery(@PathVariable Long id){
        orderService.updateDelivery(id);
        return Result.success("订单状态派送中！");
    }




    /**
     * 完成订单
     * @param id
     * @return
     */
    @PutMapping("/complete/{id}")
    @ApiOperation("完成订单")
    public Result<String> complete(@PathVariable Long id){
        orderService.updateComplete(id);
        return Result.success("订单状态完成！");
    }





}
